#coding=utf-8
import requests,json,urllib
import platform_login_api
import mysql_password
def reply(phone,content):
    s = requests.session()
    userid = mysql_password.password(phone)[1]  # 获取到userid
    #对回复进行评论
    password = mysql_password.password(phone)
    # 定义headers
    headers = {'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'zh-CN,zh;q=0.9'
        ,
                'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
        , 'Referer': 'http://community-ui1-test.gdgyun.com/', 'Uid': userid,
                'Authorization': platform_login_api.login(phone)[2],
                'Origin': 'http://community-ui1-test.gdgyun.com', 'Host': 'community-article-api-test.gdgyun.com'
        , 'Content-Type': 'application/json;charset=UTF-8'}
    reply_data = ({"commentId":"135","content":content,"userId":str(userid)})
    reply = s.post('http://community-article-api-test.gdgyun.com/api/community/article/comment/reply/add',json=reply_data,headers=headers)
    msg = reply.json()["msg"]
    #判断回复评论是否成功
    if str(msg) == '发表文章评论回复成功':
        print str(msg)
    else:
        print str(msg)

def comment(phone,content):
    s = requests.session()
    userid = mysql_password.password(phone)[1]  # 获取到userid
    # 对文章进行评论
    # 定义headers
    headers1 = {'Accept': 'application/json, text/plain, */*', 'Accept-Encoding': 'gzip, deflate',
               'Accept-Language': 'zh-CN,zh;q=0.9'
        ,
               'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'
        , 'Referer': 'http://community-ui1-test.gdgyun.com/', 'Uid': userid, 'Authorization': platform_login_api.login(phone)[2],
               'Origin': 'http://community-ui1-test.gdgyun.com', 'Host': 'community-article-api-test.gdgyun.com'
        , 'Content-Type': 'application/json;charset=UTF-8'}
    comment_data = ({"content":content,"userId":str(userid),"articleId":"336"})
    comment = s.post('http://community-article-api-test.gdgyun.com/api/community/article/comment/add',json=comment_data,headers=headers1)
    msg = comment.json()["msg"]
    headers = platform_login_api.login(phone)[0]  # 获取到登录后的headers
    coinsDetail = s.get('http://community-platform-api-test.gdgyun.com/api/community/reward/silver/coinsDetail?isIncoming=',headers=headers)  # 获取银币明细
    sign_record = coinsDetail.json()["data"][0]["desc"]  # 获取desc字段值
    coins = coinsDetail.json()["data"][0]["coins"]  # 获取银币数量
    # 判断发布文章评论是否成功
    if str(msg) == '发表文章评论成功' and str(sign_record) == '发表评论' and str(coins) == '3':
        print str(msg)+u'获得银币：'+str(coins)
    else:
        print str(msg)

if __name__ == '__main__':
     # for i in range(3,10,1):
     #     reply('17700010004','回复评论0'+str(i))
    for i in range(0,10,1):
        comment('17781108472','测试评论测试00'+str(i))